package com.ruizhong.centercontroll.controller;

import com.ruizhong.centercontroll.service.*;
import com.ruizhong.centercontroll.util.Result;
import com.ruizhong.centercontroll.util.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.EncodeException;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/command/api")
@CrossOrigin
@Slf4j
public class CommandController {

    @Autowired
    private TSliderService tSliderService;

    @Autowired
    private TButtonService tButtonService;

    @Autowired
    private TSwitchService tSwitchService;
    
    @Autowired
    private TAudioService tAudioService;

    @Autowired
    private TVideoService tVideoService;

    public static ConcurrentHashMap<String,String> runMap = new ConcurrentHashMap<>();

    @PostMapping("/executeCommand")
    public Result executeCommand(@RequestBody Map<String, Object> conditionMap, HttpServletRequest request ) throws EncodeException, IOException {
        String ip = getClientIp(request);
        synchronized (ip.intern()) {
            ConcurrentHashMap.KeySetView<String, String> ips = runMap.keySet();
            if (ips.contains(ip)){
                throw new RuntimeException("命令执行中");
            }
            runMap.put(ip,"");
        }
        Map<String,Object> resMap = null;
        switch (conditionMap.get("type").toString()) {
            case "BUTTON":
                tButtonService.executeCommand(conditionMap,ip);
                break;
            case "video":
                tVideoService.executeCommand(conditionMap,ip);
                break;
            case "IMAGE":
                break;
            case "SLIDER":
                 tSliderService.executeCommand(conditionMap,ip);
                break;
            case "SWITCH":
                 tSwitchService.executeCommand(conditionMap,ip);
                break;
            case "TEXTVIEW":
                break;
            case "AUDIO":
                tAudioService.executeCommand(conditionMap,ip);
                break;
        }
        return new Result(ResultCode.success, null, null);
    }

    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();  // 直接连接的IP
        }
        // 处理多级代理（取第一个非unknown的IP）
        if (ip != null && ip.contains(",")) {
            ip = ip.split(",")[0].trim();
        }
        // 本地测试时处理IPv6地址
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "127.0.0.1";
        }
        return ip;
    }

}
